package data;

import java.util.ArrayList;
import services.RequestService;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.rentapark.app.R;
import android.content.DialogInterface;

//this is a class that extends ItemizedOverlay<OverlayItem>
public class MyOverlays extends ItemizedOverlay<OverlayItem>{

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	public static Context mContext;
	public DB parking_db;

	public MyOverlays (Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public MyOverlays (Drawable defaultMarker, Context context) {
		super(defaultMarker);
		mContext = context;
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	//method handling onTap
	protected boolean onTap(final int index) {
		parking_db=DB.getInstance();
		OverlayItem item = mOverlays.get(index);

		AlertDialog dialog = new AlertDialog.Builder(mContext).create();

		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		if(parking_db.dealClosed==false)//if a deal wasn't closed yet...
		{
					dialog.setButton("Request", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Log.d("test","perform request");
				
				
				int value=0;
				
				//search for the index of the overlay in the local DB
				GeoPoint point=mOverlays.get(index).getPoint();
				double lat=(point.getLatitudeE6())/1E6;
				double longi=(point.getLongitudeE6())/1E6;
				for (int i=0;i<parking_db.dbList.list.size();i++)
				{
					if((parking_db.dbList.list.get(i).parkingCordinate.latitude==lat)&&(parking_db.dbList.list.get(i).parkingCordinate.longitude==longi))
					{
						value=i;//searching for the parking spot's price
						break;
					}
				}
				if(parking_db.userCredit>=parking_db.dbList.list.get(value).prize)//checking if the user has enough credit to request the parking spot
				{
					Drawable marker=mContext.getResources().getDrawable(R.drawable.red);//set a diff icon for a requested spot
					marker.setBounds(
							0 - marker.getIntrinsicWidth() / 2, 0 - marker.getIntrinsicHeight(), 
							marker.getIntrinsicWidth() / 2, 0);
					mOverlays.get(index).setMarker(marker);
				}

				Intent myIntent = new Intent(MyOverlays.mContext, RequestService.class);//start request service
				Bundle bundle = new Bundle();
				bundle.putInt("index", value);
				myIntent.putExtras(bundle);
				mContext.startService(myIntent);
			}

		});
		}

		dialog.show();
		return true;
	}
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);
		//boundCenterBottom(marker);
	}

}


